//
//  MAPYHudCommon.m
//  Mars Proxy
//
//  Created by PowerfullClean on 2023/10/25.
//

#import "MAPYHudCommon.h"
#import "Mars_Proxy-Swift.h"
@implementation MAPYHudCommon
@synthesize showMessage;
+ (MAPYHudCommon *)shareInstance{
    static MAPYHudCommon *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[MAPYHudCommon alloc] init];
    });
    return sharedInstance;
}
- (void)showDuration{
    [showMessage hideAnimated:YES afterDelay:2.0f];
}

- (void)buildMessage:(NSString *)message view:(UIView*)view  isError:(BOOL)yesOrNo{
    [showMessage removeFromSuperview];
    showMessage=[[MBProgressHUD alloc] initWithView:view];
    showMessage.animationType = MBProgressHUDAnimationZoom;
    if(!yesOrNo)
        [showMessage setCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mapy_hud_successfully"]]];
    else
        [showMessage setCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error.png"]]];
    [showMessage setMode:MBProgressHUDModeCustomView];
    [view addSubview:showMessage];
    showMessage.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:1];
    showMessage.contentColor = [UIColor whiteColor];
    showMessage.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    showMessage.removeFromSuperViewOnHide = YES;
    showMessage.detailsLabel.text = message;
    [showMessage showAnimated:YES];
    [self performSelectorOnMainThread:@selector(showDuration) withObject:nil waitUntilDone:YES];
}

- (void)buildMessage:(NSString *)message isError:(BOOL)yesOrNo{
    [showMessage removeFromSuperview];
    showMessage=[[MBProgressHUD alloc] initWithView:[self getTopView]];
    showMessage.animationType = MBProgressHUDAnimationZoom;
    if(!yesOrNo)
        [showMessage setCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mapy_hud_successfully"]]];
    else
        [showMessage setCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error.png"]]];
    [showMessage setMode:MBProgressHUDModeCustomView];
    [[self getTopView] addSubview:showMessage];
    showMessage.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:1];
    showMessage.contentColor = [UIColor whiteColor];
    showMessage.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    showMessage.removeFromSuperViewOnHide = YES;
    showMessage.detailsLabel.text=message;
    [showMessage showAnimated:YES];
    [self performSelectorOnMainThread:@selector(showDuration) withObject:nil waitUntilDone:YES];
}

//- (UIWindow *)getTopLevelWindow{
//    UIWindow *window=nil;
//    for (UIWindow *_window in [[UIApplication sharedApplication] windows]) {
//        NSLog(@"%f",_window.windowLevel);
//        if(window==nil){
//            window=_window;
//        }
//        if(_window.windowLevel>window.windowLevel){
//            window=_window;
//        }
//    }
//    return window;
//}

- (UIView *)getTopView{
    return [MAPYAppDelegate mapy_shared].mapy_window;
}

- (void)buildProcessMessage:(NSString *)message{
    [showMessage removeFromSuperview];
    showMessage=[[MBProgressHUD alloc] initWithView:[self getTopView]];
    showMessage.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:1];
    showMessage.contentColor = [UIColor whiteColor];
    showMessage.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    showMessage.removeFromSuperViewOnHide = YES;
    [[self getTopView] addSubview:showMessage];
    showMessage.label.text=message;
    [showMessage showAnimated:YES];
}

- (void)buildProcessMessage:(NSString *)message  view:(UIView*)view{
    [showMessage removeFromSuperview];
    showMessage=[[MBProgressHUD alloc] initWithView:view];
    showMessage.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:1];
    showMessage.contentColor = [UIColor whiteColor];
    showMessage.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    showMessage.removeFromSuperViewOnHide = YES;
    [view addSubview:showMessage];
    showMessage.label.text=message;
    [showMessage showAnimated:YES];
}

+ (void)mapy_showMessage:(NSString *)message view:(UIView*)view isError:(BOOL)yesOrNo{
    MAPYHudCommon *sharedInstance =[MAPYHudCommon shareInstance];
    [sharedInstance buildMessage:message view:view isError:yesOrNo];
}

+ (void)mapy_showMessage:(NSString *)message isError:(BOOL)yesOrNo{
    MAPYHudCommon *sharedInstance =[MAPYHudCommon shareInstance];
    [sharedInstance buildMessage:message isError:yesOrNo];
}

+ (MBProgressHUD *)mapy_showProcessMessage:(NSString *)message{
    MAPYHudCommon *sharedInstance =[MAPYHudCommon shareInstance];
    [sharedInstance buildProcessMessage:message];
    return sharedInstance.showMessage;
}

+ (MBProgressHUD *)mapy_showProcessMessage:(NSString *)message view:(UIView*)view{
    MAPYHudCommon *sharedInstance =[MAPYHudCommon shareInstance];
    [sharedInstance buildProcessMessage:message view:view];
    return sharedInstance.showMessage;
}

@end

@implementation MBProgressHUD (hidden)

- (void)mapy_hideAnimation:(BOOL)animation{
    [self hideAnimated:animation];
}

@end
